// lib/presentation/screens/login_screen.dart
import 'package:basic_ui/constants/app_sizes.dart';
import 'package:basic_ui/localization/string_hardcoded.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:go_router/go_router.dart';
import '../../../../routing/app_router.dart';
import '../../data/models/user.dart';
import '../controllers/auth_controller.dart';

class CustomLoginScreen extends ConsumerStatefulWidget {
  const CustomLoginScreen({super.key});

  @override
  _CustomLoginScreenState createState() => _CustomLoginScreenState();
}

class _CustomLoginScreenState extends ConsumerState<CustomLoginScreen> {
  final TextEditingController usernameController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    final authStatus = ref.watch(authControllerProvider);

    return Scaffold(
      appBar: AppBar(title: const Text('Login')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            Text(
              'Flutter Boot Template.\nBy InSail Working.'.hardcoded,
              style: Theme.of(context).textTheme.headlineSmall,
              textAlign: TextAlign.center,
            ),
            gapH16,
            SvgPicture.asset(
              'assets/images/insail-logo.svg',
              width: 100,
              height: 100,
              semanticsLabel: 'InSail Flutter Boot'.hardcoded,
            ),
            gapH16,
            TextField(
              controller: usernameController,
              decoration: InputDecoration(labelText: 'Username'.hardcoded),
            ),
            TextField(
              controller: passwordController,
              decoration: InputDecoration(labelText: 'Password'.hardcoded),
              obscureText: true,
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () async {
                final user = User(
                  uid: 'uid123',
                  email: usernameController.text,
                );
                final bool isSuccess =
                    await ref.read(authControllerProvider.notifier).login(user);
                if (isSuccess) {
                  // ignore: use_build_context_synchronously
                  context.goNamed(AppRoute.profile.name);
                }
              },
              child: authStatus == AuthStatus.loading
                  ? const CircularProgressIndicator()
                  : Text('Login'.hardcoded),
            ),
            if (authStatus == AuthStatus.error) ...[
              const SizedBox(height: 20),
              Text('Login Failed'.hardcoded,
                  style: const TextStyle(color: Colors.red)),
            ],
            TextButton(
              onPressed: () {
                context.goNamed(AppRoute.register.name);
              },
              child: Text('Don\'t have an account? Register'.hardcoded),
            ),
          ],
        ),
      ),
    );
  }
}
